import loginRequest from '@/request/loginRequest'
import commonRequest from '@/request/commonRequest'
import { config } from '@/config/systemConfig'

/**
 * 微站登录注册，获取openId、token等信息
 * @param request phoneNo、passwd、terminalId、deviceId、deviceModel
 */
function msClientLogin(request) {
  const reqBody = loginRequest.mixedLogin(request)
  return new Promise(function(resolve, reject) {
	  commonRequest.loginRequest(reqBody, config.LOGIN_URL_MS_CLIENT).then(res => {
      if (res.code !== 20000) {
        reject(res.message)
      }
      resolve(res.data)
	  })
  })
}

/**
 * 生活号服务号登录注册，获取openId、token等信息
 * @param request code、terminalId、deviceId、deviceModel
 */
function h5MixedLogin(request) {
  const reqBody = loginRequest.mixedLogin(request)
  return new Promise(function(resolve, reject) {
	  commonRequest.loginH5Request(reqBody).then(res => {
      if (res.code !== 20000) {
        reject(res.message)
      }
      resolve(res.data)
	  })
  })
}

/**
 * 登录注册，获取openId、token等信息
 * @param request code、terminalId、deviceId、deviceModel
 */
function miniMixedLogin(request) {
  const reqBody = loginRequest.mixedLogin(request)
  return new Promise(function(resolve, reject) {
	  commonRequest.loginRequest(reqBody).then(res => {
      if (res.code !== 20000) {
        reject(res.message)
      }
      resolve(res.data)
	  })
  })
}

/**
 * 获取令牌
 * @param {JSON} params
 */
function getTicket() {
  const reqBody = loginRequest.getTicket()
  return new Promise(function(resolve, reject) {
    commonRequest.gatewayRequest(reqBody).then((res) => {
      const data = commonRequest.handleSpIhospitalResponse(res)
      resolve(data)
    }).catch((err) => {
      reject(err)
    })
  })
}

export default {
  msClientLogin,
  h5MixedLogin,
  miniMixedLogin,
  getTicket
}
